// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import serverInfo from './store/store'

Vue.prototype.$serverInfo=serverInfo

Vue.config.productionTip = false

import elementUI from 'element-ui'
Vue.use(elementUI)
import 'element-ui/lib/theme-chalk/index.css'

import vueCookies from 'vue-cookies'
Vue.use(vueCookies)

import axios from 'axios'
Vue.prototype.$axios=axios

//配置允许前端操作Cookie
axios.defaults.withCredentials=true

//路由拦截器
router.beforeEach((to,from,next)=>{

   if(to.path=="/"){
      next()
   }else{
      let token=window.localStorage.getItem("token");
      if(token!=null&&token!=undefined&&token!=""){
         next()
      }else{
         next({path:"/"})
      }
   }

})

//axios的请求拦截器
axios.interceptors.request.use((config)=>{
  let token=window.localStorage.getItem("token")
  if(token!=null&&token!=undefined&&token!=""){
    config.headers["token"]=token;
  }
  return config;
})

//axios的响应拦截器
axios.interceptors.response.use((response)=>{

  if(response.data.code==900) {//未登录
    elementUI.Notification({title: '警告',message: '未登录或者登录过期',type: 'warning',duration: 2000})
    router.push({path:"/"})
  }
  if(response.data.code==901) {//用户名或者密码错误
    elementUI.Notification({title: '错误',message: '用户名或者密码错误',type: 'error',duration: 2000})
  }
  if(response.data.code==902) {//无权限
    elementUI.Notification({title: '警告',message: '用户无权限',type: 'warning',duration: 2000})
  }

  return response;
})


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
